<?php

class Application_Model_Data extends Application_Model_DomainObject {

    protected $_id;
    protected $_dataVal; //数据值
    protected $_dataType; //数据类型
    protected $_dataTime; //数据时间
    protected $_other; //其他
    protected $_objectType;//对象类型
    /**
     * 国际运单
     */
    const Type_Way = 1;
    /**
     * 代购订单
     */
    const Type_Order = 2;
    /**
     * 仓库物品
     */
    const Type_Store = 3;
    /**
     * 用户
     */
    const Type_User = 4;


    public static function getItemByTypeTime($dataType,$dataTime,$other) {
        return self::getDao()->queryOne(array("dataType = "=> $dataType," and dataTime = "=> $dataTime, "and other = "=> $other));
    }
    
    public static function addData($dataVal,$dataType,$dataTime,$other,$objectType) {
        $dataItem = Application_Model_Data::getItemByTypeTime($dataType, $dataTime,$other);
        if(!$dataItem) {
            $dataItem = new Application_Model_Data();
        }
        $dataItem->dataVal = $dataVal;
        $dataItem->dataType = $dataType;
        $dataItem->dataTime = $dataTime;
        $dataItem->other = $other;
        $dataItem->objectType = $objectType;
        $dataItem->save();
    }
    
    public static function DisposeOrderData($dataTime,$orderItem,$orderNewItem,$orderOldItem) {
        $user = array();            $newUser = array();         $oldUser = array();      
        $sum = 0;       $newSum = 0;        $oldSum = 0;
        $stateArr = array();  
        foreach ($orderItem as $orderKey => $orderVal) {
            $sum += $orderVal["sum"];
            $userId = $orderVal["userId"];
            $user[$userId] = true;
            $userItem = Application_Model_User::load($userId);
            $state = $userItem->CityId?$userItem->CityId:10000;
            if(array_key_exists($state, $stateArr)) {
                $stateArr[$state]["num"] = $stateArr[$state]["num"] + 1;
                $stateArr[$state]["sum"] = $stateArr[$state]["sum"] + $orderVal["sum"];
                $stateArr[$state]["user"][$userId] = true;
            } else {
                $stateArr[$state]["num"] = 1;
                $stateArr[$state]["sum"] = $orderVal["sum"];
                $stateArr[$state]["user"][$userId] = true;
            }
        }
        self::addData(count($user), Application_Model_DataType::Type_OrderUser, $dataTime, "all", self::Type_Order);
        self::addData(count($orderItem), Application_Model_DataType::Type_OrderNum, $dataTime, "all", self::Type_Order);
        self::addData($sum, Application_Model_DataType::Type_OrderSum, $dataTime, "all", self::Type_Order);
        foreach ($stateArr as $stateKey => $stateVal) {
            self::addData(count($stateVal["user"]), Application_Model_DataType::Type_OrderUser, $dataTime, $stateKey, self::Type_Order);
            self::addData($stateVal["num"], Application_Model_DataType::Type_OrderNum, $dataTime, $stateKey, self::Type_Order);
            self::addData($stateVal["sum"], Application_Model_DataType::Type_OrderSum, $dataTime, $stateKey, self::Type_Order);
        }
        foreach ($orderNewItem as $key => $orderNewVal) {
            $newSum += $orderNewVal["sum"];
            $userId = $orderNewVal["userId"];
            $newUser[$userId] = true;
        }
        self::addData(count($newUser), Application_Model_DataType::Type_OrderNewUser, $dataTime, "all", self::Type_Order);
        self::addData(count($orderNewItem), Application_Model_DataType::Type_OrderNewNum, $dataTime, "all", self::Type_Order);
        self::addData($newSum, Application_Model_DataType::Type_OrderNewSum, $dataTime, "all", self::Type_Order);
        foreach ($orderOldItem as $key => $orderOldVal) {
            $oldSum += $orderOldVal["sum"];
            $userId = $orderOldVal["userId"];
            $oldUser[$userId] = true;
        }
        self::addData(count($oldUser), Application_Model_DataType::Type_OrderOldUser, $dataTime, "all", self::Type_Order);
        self::addData(count($orderOldItem), Application_Model_DataType::Type_OrderOldNum, $dataTime, "all", self::Type_Order);
        self::addData($oldSum, Application_Model_DataType::Type_OrderOldSum, $dataTime, "all", self::Type_Order);
        return true;
    }
    
    protected function AnalysisUserId($type,$item) {
        $userId = array();
        foreach ($item as $val) {
            switch ($type) {
                case "order":
                case "way":
                    $userId[$val->userId] = true;
                    break;
                case "store":
                    $userId[$val->userid] = true;
                    break;
                default:
                    break;
            }
        }
        return $userId;
    }
    
    public static function DisposeWayData($dataTime, $wayItem, $wayNewItem, $wayOldItem) {
        $num = count($wayItem);        $sum = 0;      $user = array();      $weight = 0;     $volume = 0;
        $oldNum = count($wayOldItem);        $oldSum = 0;        $oldUser = array();     $oldWeight = 0;     $oldVolume = 0;
        $newNum = count($wayNewItem);        $newSum = 0;        $newUser = array();     $newWeight = 0;     $newVolume = 0;
        $stateArr = array();
        foreach ($wayItem as $key => $wayVal) {
            $wayItem = Application_Model_wayBillItem::load($wayVal["id"]);
            $EstimateSum = $wayItem->getFeeByType(Application_Model_FeeType::EstimateSum);
            $productWeight = $wayItem->getWeightByType(Application_Model_WeightType::ProductWeight);
            $EstimateVolume = $wayItem->getWeightByType(Application_Model_WeightType::EstimateVolume);
            $bagWeight = $wayItem->getWeightByType(Application_Model_WeightType::BagWeight);
            $sum += $EstimateSum;
            $user[$wayItem->UserId] = true;
            $weight += $productWeight+$bagWeight;
            $volume += $EstimateVolume;
            if(array_key_exists($wayItem->state, $stateArr)) {
                $stateArr[$wayItem->state]["num"] = $stateArr[$wayItem->state]["num"] + 1;
                $stateArr[$wayItem->state]["sum"] = $stateArr[$wayItem->state]["sum"] + $EstimateSum;
                $stateArr[$wayItem->state]["user"][$wayItem->UserId] = true;
                $stateArr[$wayItem->state]["weight"] = $stateArr[$wayItem->state]["weight"] + $productWeight + $bagWeight;
                $stateArr[$wayItem->state]["volume"] = $stateArr[$wayItem->state]["volume"] + $EstimateVolume;
            } else {
                $stateArr[$wayItem->state]["num"] = 1;
                $stateArr[$wayItem->state]["sum"] = $EstimateSum;
                $stateArr[$wayItem->state]["user"][$wayItem->UserId] = true;
                $stateArr[$wayItem->state]["weight"] = $productWeight + $bagWeight;
                $stateArr[$wayItem->state]["volume"] = $EstimateVolume;
            }
        }
        foreach ($wayNewItem as $key => $wayVal) {
            $wayItem = Application_Model_wayBillItem::load($wayVal["id"]);
            $EstimateSum = $wayItem->getFeeByType(Application_Model_FeeType::EstimateSum);
            $productWeight = $wayItem->getWeightByType(Application_Model_WeightType::ProductWeight);
            $EstimateVolume = $wayItem->getWeightByType(Application_Model_WeightType::EstimateVolume);
            $bagWeight = $wayItem->getWeightByType(Application_Model_WeightType::BagWeight);
            $newSum += $EstimateSum;
            $newUser[$wayItem->UserId] = true;
            $newWeight += $productWeight+$bagWeight;
            $newVolume += $EstimateVolume;
        }
        foreach ($wayOldItem as $key => $wayVal) {
            $wayItem = Application_Model_wayBillItem::load($wayVal["id"]);
            $EstimateSum = $wayItem->getFeeByType(Application_Model_FeeType::EstimateSum);
            $productWeight = $wayItem->getWeightByType(Application_Model_WeightType::ProductWeight);
            $EstimateVolume = $wayItem->getWeightByType(Application_Model_WeightType::EstimateVolume);
            $bagWeight = $wayItem->getWeightByType(Application_Model_WeightType::BagWeight);
            $oldSum += $EstimateSum;
            $oldUser[$wayItem->UserId] = true;
            $oldWeight += $productWeight+$bagWeight;
            $oldVolume += $EstimateVolume;
        }
        self::addData(count($user), Application_Model_DataType::Type_WayUser, $dataTime, "all", self::Type_Way);
        self::addData($num, Application_Model_DataType::Type_WayNum, $dataTime, "all", self::Type_Way);
        self::addData($sum, Application_Model_DataType::Type_WayMoney, $dataTime, "all", self::Type_Way);
        self::addData($weight, Application_Model_DataType::Type_WayWeight, $dataTime, "all", self::Type_Way);
        self::addData($volume, Application_Model_DataType::Type_WayVolume, $dataTime, "all", self::Type_Way);
        self::addData(count($newUser), Application_Model_DataType::Type_WayNewUser, $dataTime, "all", self::Type_Way);
        self::addData($newNum, Application_Model_DataType::Type_WayNewNum, $dataTime, "all", self::Type_Way);
        self::addData($newSum, Application_Model_DataType::Type_WayNewMoney, $dataTime, "all", self::Type_Way);
        self::addData($newWeight, Application_Model_DataType::Type_WayNewWeight, $dataTime, "all", self::Type_Way);
        self::addData($newVolume, Application_Model_DataType::Type_WayNewVolume, $dataTime, "all", self::Type_Way);
        self::addData(count($oldUser), Application_Model_DataType::Type_WayOldUser, $dataTime, "all", self::Type_Way);
        self::addData($oldNum, Application_Model_DataType::Type_WayOldNum, $dataTime, "all", self::Type_Way);
        self::addData($oldSum, Application_Model_DataType::Type_WayOldMoney, $dataTime, "all", self::Type_Way);
        self::addData($oldWeight, Application_Model_DataType::Type_WayOldWeight, $dataTime, "all", self::Type_Way);
        self::addData($oldVolume, Application_Model_DataType::Type_WayOldVolume, $dataTime, "all", self::Type_Way);
        foreach ($stateArr as $stateKey => $stateVal) {
            self::addData(count($stateVal["user"]), Application_Model_DataType::Type_WayUser, $dataTime, $stateKey, self::Type_Way);
            self::addData($stateVal["num"], Application_Model_DataType::Type_WayNum, $dataTime, $stateKey, self::Type_Way);
            self::addData($stateVal["sum"], Application_Model_DataType::Type_WayMoney, $dataTime, $stateKey, self::Type_Way);
            self::addData($stateVal["weight"], Application_Model_DataType::Type_WayWeight, $dataTime, $stateKey, self::Type_Way);
            self::addData($stateVal["volume"], Application_Model_DataType::Type_WayVolume, $dataTime, $stateKey, self::Type_Way);
        }
        return true;
    }
    
    public static function DisposeStoreData($dataTime, $storeItem, $storeNewItem, $storeOldItem) {
        $num = count($storeItem);          $weight = 0;        $user = array();
        $newNum = count($storeNewItem);    $newWeight = 0;     $newUser = array();
        $oldNum = count($storeOldItem);    $oldWeight = 0;     $oldUser = array();
        $stateArr = array();
        foreach ($storeItem as $key => $storeVal) {
            $weight += $storeVal["weight"];
            $userId = $storeVal["userid"];
            $userItem = Application_Model_User::load($userId);
            $state = $userItem->cityId?$userItem->cityId:10000;
            $user[$userId] = true;
            if(array_key_exists($state, $stateArr)) {
                $stateArr[$state]["num"] = $stateArr[$state]["num"] + 1;
                $stateArr[$state]["weight"] = $stateArr[$state]["weight"] + $storeVal["weight"];
                $stateArr[$state]["user"][$userId] = true;
            } else {
                $stateArr[$state]["num"] = 1;
                $stateArr[$state]["weight"] = $storeVal["weight"];
                $stateArr[$state]["user"][$userId] = true;
            }
        }
        foreach ($storeOldItem as $key => $storeVal) {
            $oldWeight += $storeVal["weight"];
            $userId = $storeVal["userid"];
            $oldUser[$userId] = true;
        }
        foreach ($storeNewItem as $key => $storeVal) {
            $newWeight += $storeVal["weight"];
            $userId = $storeVal["userid"];
            $newUser[$userId] = true;
        }
        self::addData(count($user), Application_Model_DataType::Type_StoreUser, $dataTime, "all", self::Type_Store);
        self::addData($num, Application_Model_DataType::Type_StoreNum, $dataTime, "all", self::Type_Store);
        self::addData($weight, Application_Model_DataType::Type_StoreWeight, $dataTime, "all", self::Type_Store);
        self::addData(count($newUser), Application_Model_DataType::Type_StoreNewUser, $dataTime, "all", self::Type_Store);
        self::addData($newNum, Application_Model_DataType::Type_StoreNewNum, $dataTime, "all", self::Type_Store);
        self::addData($newWeight, Application_Model_DataType::Type_StoreNewWeight, $dataTime, "all", self::Type_Store);
        self::addData(count($oldUser), Application_Model_DataType::Type_StoreOldUser, $dataTime, "all", self::Type_Store);
        self::addData($oldNum, Application_Model_DataType::Type_StoreOldNum, $dataTime, "all", self::Type_Store);
        self::addData($oldWeight, Application_Model_DataType::Type_StoreOldWeight, $dataTime, "all", self::Type_Store);
        foreach ($stateArr as $stateKey => $stateVal) {
            self::addData(count($stateVal["user"]), Application_Model_DataType::Type_StoreUser, $dataTime, $stateKey, self::Type_Store);
            self::addData($stateVal["num"], Application_Model_DataType::Type_StoreNum, $dataTime, $stateKey, self::Type_Store);
            self::addData($stateVal["weight"], Application_Model_DataType::Type_StoreWeight, $dataTime, $stateKey, self::Type_Store);
        }
        return true;
    }
    
    public static function disposeUserData($dataTime,$userItem) {
        $stateArr = array();
        foreach ($userItem as $userVal) {
            $state = $userVal->cityId?$userVal->cityId:10000;
            $stateArr[$state]["user"][$userVal->id] = true;
        }
        self::addData(count($userItem), Application_Model_DataType::Type_UserNum, $dataTime, "all", self::Type_User);
        foreach ($stateArr as $stateKey => $stateVal) {
            self::addData(count($stateVal["user"]), Application_Model_DataType::Type_UserNum, $dataTime, $stateKey, self::Type_User);
        }
        return true;
    }
    
    public function getSumData($startTime,$entTime,$dataType,$other) {
        $where = array();
        $and = "";
        if($startTime != null) {
            $where[$and." dataTime >= "] = $startTime;
            $and = " and ";
        }
        if($entTime != null) {
            $where[$and." dataTime <= "] = $entTime;
            $and = " and ";
        }
        if($dataType != null) {
            $where[$and." dataType = "] = $dataType;
            $and = " and ";
        }
        if($other != null) {
            $where[$and." other = "] = $other;
            $and = " and ";
        }
        $sum = self::getDao()->sum($where, "dataVal");
        return $sum?$sum:0;
    }
    
    public function getCityName($cityId) {
        $cityItem = Application_Model_city::load($cityId);
        return $cityItem?$cityItem->Name:"未知地方";
    }
}